package com.uusee.util.regex;

import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

public class RegexUtils {
	public static String getValue(String regex, String text, int gounp) {
		String value = "";

		try {
			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(regex, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(text);
			if (matcher.contains(input, pattern)) {
				MatchResult m = matcher.getMatch();
				value = m.group(gounp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return value;
	}
	
	public static boolean isMatch(String regex, String text) {
		boolean match =false;

		try {
			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(regex, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(text);
			if (matcher.contains(input, pattern)) {
				match =true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			match =false;
		}

		return match;
	}
}
